<template>
    <mescroll-uni @init="mescrollInit" :up="upOption" :down="downOption" @down="downCallback" @up="upCallback">
        <view class="content">
            <view :style='{"minHeight":"100%","width":"100%","padding":"0 20rpx","position":"relative","background":"#F3F3F3","height":"auto"}'>
                <view class="cu-bar bg-white search" :style='{"padding":"20rpx 0 0 0","margin":"0","background":"none","display":"flex","width":"100%","position":"relative","height":"auto"}'>
                    <view :style='{"border":"none","margin":"0","borderRadius":"20rpx","flex":"1","background":"#fff","lineHeight":"80rpx","position":"relative","height":"80rpx"}' class="search-form round">
                        <text class="icon iconfont icon-fangdajing07" :style='{"color":"rgb(153, 153, 153)","left":"0px","textAlign":"center","width":"80rpx","fontSize":"40rpx","lineHeight":"80rpx","position":"absolute","right":"0px"}'></text>
                        <input placeholder-style="color: #333" :style='{"border":"0","padding":"12rpx 20rpx 12rpx 80rpx","color":"#333","borderRadius":"40rpx","background":"none","width":"100%","lineHeight":"80rpx","fontSize":"28rpx","height":"80rpx"}' v-model="searchForm.lipinmingcheng" type="text" placeholder="礼品名称" ></input>
                    </view>
                    <button :style='{"border":"0","padding":"0px","margin":"0","color":"#fff","right":"4rpx","borderRadius":"20rpx","top":"24rpx","background":"linear-gradient( 135deg, #9EEEEB 0%, #99ABFF 100%)","width":"136rpx","lineHeight":"70rpx","fontSize":"28rpx","position":"absolute","height":"70rpx","zIndex":"99"}' @tap="search" class="cu-btn shadow-blur round">搜索</button>
                </view>

                <view :style='{"flexWrap":"wrap","margin":"20rpx 0","display":"flex"}'>
                    <view @click="sortClick('addtime')" :style='{"border":"2rpx solid rgb(153, 152, 152)","padding":"0 12rpx","margin":"0 20rpx 0 0","outline":"0","borderRadius":"8rpx","background":"#E3E3E3","display":"flex"}'>
                        <text :style='{"color":"#333","lineHeight":"48rpx","fontSize":"24rpx"}'>按日期</text>
                        <text v-if="listSort!='addtime'" class="icon iconfont icon-shijian18" :style='{"margin":"0 4rpx 0 0","lineHeight":"48rpx","fontSize":"24rpx","color":"#333"}'></text>
                        <text v-else-if="listSort=='addtime'&&listOrder=='asc'" class="icon iconfont icon-shijian18" :style='{"margin":"0 4rpx 0 0","lineHeight":"48rpx","fontSize":"24rpx","color":"#333"}'></text>
                        <text v-else-if="listSort=='addtime'&&listOrder=='desc'" class="icon iconfont icon-shijian18" :style='{"margin":"0 4rpx 0 0","lineHeight":"48rpx","fontSize":"24rpx","color":"#333"}'></text>
                    </view>
                </view>
                <view :style='{"width":"100%","background":"none","flexDirection":"column","display":"flex","height":"auto"}'>
                    <!-- 样式1 -->
                    <view class="uni-product-list" :style='{"padding":"0","margin":"40rpx 0 0","flexWrap":"wrap","flex":"auto","display":"flex","width":"100%","justifyContent":"space-between","height":"auto"}'>
                        <view @tap="onDetailTap(product)" class="uni-product" :style='{"boxShadow":"0 2rpx 8rpx rgba(0,0,0,.1)","margin":"0 0 20rpx","backgroundColor":"#fff","borderRadius":"8rpx","flexWrap":"wrap","display":"flex","width":"48%","height":"auto"}' v-for="(product,index) in list" :key="index">
                            <view class="uni-product-title" :style='{"padding":"4rpx 20rpx","overflow":"hidden","whiteSpace":"nowrap","color":"#333","maxHeight":"96rpx","width":"100%","lineHeight":"48rpx","fontSize":"28rpx","textOverflow":"ellipsis"}'>{{product.lipinmingcheng}}</view>
                            <image :style='{"padding":"0","margin":"0","objectFit":"cover","borderRadius":"8rpx 8rpx 0 0","display":"block","width":"100%","height":"300rpx","order":"-1"}' mode="aspectFill" class="uni-product-image" v-if="preHttp(product.lipintupian)" :src="product.lipintupian.split(',')[0]"></image>
                            <image :style='{"padding":"0","margin":"0","objectFit":"cover","borderRadius":"8rpx 8rpx 0 0","display":"block","width":"100%","height":"300rpx","order":"-1"}' mode="aspectFill" class="uni-product-image" v-else :src="product.lipintupian?baseUrl+product.lipintupian.split(',')[0]:''"></image>

                            <view :style='{"border":"0px solid rgb(162, 144, 104)","padding":"0 20rpx","margin":"20rpx 0 0","textAlign":"center"}'>
                                <text class="icon iconfont icon-shijian21" :style='{"margin":"0 4rpx 0 0","lineHeight":"1.5","fontSize":"24rpx","color":"#666"}'></text>
                                <text :style='{"color":"#666","lineHeight":"1.5","fontSize":"24rpx"}'>{{product.addtime.split(' ')[0].replace(/\-/g,'-')}}</text>
                            </view>
                            <view :style='{"padding":"0 20rpx"}'>
                                <text class="icon iconfont icon-geren16" :style='{"margin":"0 4rpx 0 0","lineHeight":"1.5","fontSize":"24rpx","color":"#666"}'></text>
                                <text :style='{"color":"#666","lineHeight":"1.5","fontSize":"24rpx"}'>{{product.zhanghao ? product.zhanghao : product.yuangongzhanghao}}</text>
                            </view>
                            <!-- #ifdef MP-WEIXIN -->
                            <view :style='{"width":"100%","padding":"8rpx 0 20rpx","justifyContent":"center","display":"flex","height":"auto"}'>
                                <view :style='{"padding":"8rpx 12rpx","margin":"0 20rpx 0 0","background":"#5ba6df","display":"flex"}' v-if="(userid && isAuth('duihuandingdan','修改')) || (!userid && isAuthFront('duihuandingdan','修改'))" @tap.stop.proevent="onUpdate" :data-row="product">
                                    <text :style='{"margin":"0 8rpx 0 0","fontSize":"24rpx","lineHeight":"1","color":"#fff","display":"inline-block"}' class="cuIcon-edit"></text>
                                    <text :style='{"fontSize":"24rpx","lineHeight":"1","color":"#fff","display":"inline-block"}'>修改</text>
                                </view>
                                <view :style='{"padding":"8rpx 12rpx","background":"#df5b5b","display":"flex"}' v-if="(userid && isAuth('duihuandingdan','删除')) || (!userid && isAuthFront('duihuandingdan','删除'))" @tap.stop.proevent="onDelete" :data-row="product">
                                    <text :style='{"margin":"0 8rpx 0 0","fontSize":"24rpx","lineHeight":"1","color":"#fff","display":"inline-block"}' class="cuIcon-delete"></text>
                                    <text :style='{"fontSize":"24rpx","lineHeight":"1","color":"#fff","display":"inline-block"}'>删除</text>
                                </view>
                            </view>
                            <!-- #endif -->
                            <!-- #ifndef MP-WEIXIN -->
                            <view :style='{"width":"100%","padding":"8rpx 0 20rpx","justifyContent":"center","display":"flex","height":"auto"}'>
                                <view :style='{"padding":"8rpx 12rpx","margin":"0 20rpx 0 0","background":"#5ba6df","display":"flex"}' v-if="(userid && isAuth('duihuandingdan','修改')) || (!userid && isAuthFront('duihuandingdan','修改'))" @tap.stop.proevent="onUpdateTap(product)">
                                    <text :style='{"margin":"0 8rpx 0 0","fontSize":"24rpx","lineHeight":"1","color":"#fff","display":"inline-block"}' class="cuIcon-edit"></text>
                                    <text :style='{"fontSize":"24rpx","lineHeight":"1","color":"#fff","display":"inline-block"}'>修改</text>
                                </view>
                                <view :style='{"padding":"8rpx 12rpx","background":"#df5b5b","display":"flex"}' v-if="(userid && isAuth('duihuandingdan','删除')) || (!userid && isAuthFront('duihuandingdan','删除'))" @tap.stop.proevent="onDeleteTap(product.id)">
                                    <text :style='{"margin":"0 8rpx 0 0","fontSize":"24rpx","lineHeight":"1","color":"#fff","display":"inline-block"}' class="cuIcon-delete"></text>
                                    <text :style='{"fontSize":"24rpx","lineHeight":"1","color":"#fff","display":"inline-block"}'>删除</text>
                                </view>
                            </view>
                            <!-- #endif -->
                        </view>
                    </view>
                </view>
                <button :style='{"border":"0","boxShadow":"0 2rpx 12rpx rgba(0,0,0,.3)","color":"rgb(255, 255, 255)","bottom":"0","right":"0","outline":"none","borderRadius":"20rpx","background":"linear-gradient( 135deg, #9EEEEB 0%, #99ABFF 100%)","width":"120rpx","lineHeight":"70rpx","fontSize":"28rpx","position":"relative","height":"70rpx","zIndex":"1"}' v-if="userid && isAuth('duihuandingdan','新增')" class="add-btn" @click="onAddTap()">新增</button>
                <button :style='{"border":"0","boxShadow":"0 2rpx 12rpx rgba(0,0,0,.3)","color":"rgb(255, 255, 255)","bottom":"0","right":"0","outline":"none","borderRadius":"20rpx","background":"linear-gradient( 135deg, #9EEEEB 0%, #99ABFF 100%)","width":"120rpx","lineHeight":"70rpx","fontSize":"28rpx","position":"relative","height":"70rpx","zIndex":"1"}' v-if="!userid && isAuthFront('duihuandingdan','新增')" class="add-btn" @click="onAddTap()">新增</button>
            </view>
        </view>
    </mescroll-uni>
</template>

<script>
    export default {
        data() {
            return {
                btnColor: ['#409eff','#67c23a','#909399','#e6a23c','#f56c6c','#356c6c','#351c6c','#f093a9','#a7c23a','#104eff','#10441f','#a21233','#503319'],
                list: [],
                lists: [],
                userid: '',
                currentUser: {}, // 当前登录用户信息
                mescroll: null, //mescroll实例对象
                downOption: {
                    auto: false //是否在初始化后,自动执行下拉回调callback; 默认true
                },
                upOption: {
                    noMoreSize: 5, //如果列表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看; 默认5
                    textNoMore: '~ 没有更多了 ~',
                },
                hasNext: true,
                searchForm:{
                    lipinmingcheng: '',
                },
                CustomBar: '0',
                listSort: 'id',
                listOrder: 'desc',
                screenBoxShow: false,
            };
        },
        watch: {
        },
        mounted() {
        },
        computed: {
            baseUrl() {
                return this.$base.url;
            },
        },
        async onShow() {
            this.btnColor = this.btnColor.sort(()=> {
                return (0.5-Math.random());
            });
            this.hasNext = true
            // 重新加载数据
            if (this.mescroll) this.mescroll.resetUpScroll()
        },
        async onLoad(options) {
            // 获取当前登录用户信息
            const userSession = uni.getStorageSync('userSession');
            if (userSession) {
                this.currentUser = JSON.parse(userSession);
                // console.log('当前登录用户:', this.currentUser);
            }
            
            if(options.userid) {
                this.userid = options.userid;
            } else {
                this.userid = "";
            }
            this.hasNext = true
            // 重新加载数据
            if (this.mescroll) this.mescroll.resetUpScroll()
        },
        components: {
        },
        methods: {
            screenReset(){
                this.searchForm = {}
                this.search()
                this.$forceUpdate()
            },
            uGetRect(selector, all) {
                return new Promise(resolve => {
                    uni.createSelectorQuery()
                    .in(this)
                    [all ? 'selectAll' : 'select'](selector)
                    .boundingClientRect(rect => {
                        if (all && Array.isArray(rect) && rect.length) {
                            resolve(rect);
                        }
                        if (!all && rect) {
                            resolve(rect);
                        }
                    })
                    .exec();
                });
            },
            cloneData(data) {
                return JSON.parse(JSON.stringify(data));
            },
            sortClick(type){
                if(this.listSort==type){
                    if(this.listOrder == 'desc'){
                        this.listOrder = 'asc'
                    }else{
                        this.listOrder = 'desc'
                    }
                }else{
                    this.listSort = type
                    this.listOrder = 'desc'
                }
                this.search()
            },
            priceChange(price) {
                return Number(price).toFixed(2);
            },
            preHttp(str) {
                return str && str.substr(0,4)=='http';
            },
            //mescroll组件初始化的回调,可获取到mescroll对象
            mescrollInit(mescroll) {
                this.mescroll = mescroll;
            },
            /*下拉刷新的回调 */
            downCallback(mescroll) {
                this.hasNext = true
                // 重置分页参数页数为1
                mescroll.resetUpScroll()
            },
            /*上拉加载的回调: mescroll携带page的参数, 其中num:当前页 从1开始, size:每页数据条数,默认10 */
            async upCallback(mescroll) {
                let params = {
                    page: mescroll.num,
                    limit: mescroll.size,
                    // 根据当前登录用户的角色，添加对应的账号过滤条件
                    ...(this.currentUser.zhanghao ? { zhanghao: this.currentUser.zhanghao } : {}),
                    ...(this.currentUser.yuangongzhanghao ? { yuangongzhanghao: this.currentUser.yuangongzhanghao } : {}),
                };
                params['sort'] = this.listSort;
                params['order'] = this.listOrder;

                if (this.searchForm.lipinmingcheng) {
                    params['lipinmingcheng'] = '%' + this.searchForm.lipinmingcheng + '%';
                }

                // 统一使用page接口（根据实际情况调整）
                const res = await this.$api.page(`duihuandingdan`, params);

                // 如果是第一页数据置空
                if (mescroll.num === 1) this.list = [];
                this.list = this.list.concat(res.data.list);
                this.$forceUpdate();

                let length = Math.ceil(this.list.length / 6);
                let arr = [];
                for (let i = 0; i < length; i++) {
                    arr[i] = this.list.slice(i * 6, (i + 1) * 6);
                }
                this.lists = arr;
                if (res.data.list.length === 0) this.hasNext = false;
                mescroll.endSuccess(mescroll.size, this.hasNext);
            },
            // 详情
            onDetailTap(item) {
                uni.setStorageSync("useridTag",this.userid);
                this.$utils.jump(`./detail?id=${item.id}&userid=`+this.userid)
            },
            onUpdate(e){
                this.onUpdateTap(e.currentTarget.dataset.row)
            },
            // 修改
            onUpdateTap(row){
                if(row.sfsh=='是'||row.sfsh=='否'){
                    this.$utils.msg('已审核完成,不能修改');
                    return false
                }
                uni.setStorageSync("useridTag",this.userid);
                this.$utils.jump(`./add-or-update?id=${row.id}`)
            },
            // 添加
            onAddTap(){
                uni.setStorageSync("useridTag",this.userid);
                this.$utils.jump(`./add-or-update`)
            },
            onDelete(e){
                this.onDeleteTap(e.currentTarget.dataset.row.id)
            },
            onDeleteTap(id){
                var _this = this;
                uni.showModal({
                    title: '提示',
                    content: '是否确认删除',
                    success: async function(res) {
                        if (res.confirm) {
                            await _this.$api.del('duihuandingdan', JSON.stringify([id]));
                            _this.$utils.msg('删除成功');
                            _this.hasNext = true
                            // 重置分页参数页数为1
                            _this.search()
                        }
                    }
                });
            },
            // 搜索
            async search() {
                this.mescroll.num = 1;
                let searchForm = {
                    page: this.mescroll.num,
                    limit: this.mescroll.size,
                    // 根据当前登录用户的角色，添加对应的账号过滤条件
                    ...(this.currentUser.zhanghao ? { zhanghao: this.currentUser.zhanghao } : {}),
                    ...(this.currentUser.yuangongzhanghao ? { yuangongzhanghao: this.currentUser.yuangongzhanghao } : {}),
                };
                searchForm['sort'] = this.listSort;
                searchForm['order'] = this.listOrder;

                if (this.searchForm.lipinmingcheng) {
                    searchForm['lipinmingcheng'] = '%' + this.searchForm.lipinmingcheng + '%';
                }

                // 统一使用page接口（根据实际情况调整）
                const res = await this.$api.page(`duihuandingdan`, searchForm);

                // 如果是第一页数据置空
                if (this.mescroll.num === 1) this.list = [];
                this.list = this.list.concat(res.data.list);

                let length = Math.ceil(this.list.length / 6);
                let arr = [];
                for (let i = 0; i < length; i++) {
                    arr[i] = this.list.slice(i * 6, (i + 1) * 6);
                }
                this.lists = arr;
                if (res.data.list.length === 0) this.hasNext = false;
                this.mescroll.endSuccess(this.mescroll.size, this.hasNext);
                this.screenBoxShow = false;
            }
        }
    };
</script>

<style lang="scss" scoped>
    .content {
        min-height: calc(100vh - 44px);
        box-sizing: border-box;
    }
    .category-one .tab {
        cursor: pointer;
        border-radius: 30rpx;
        padding: 0 12rpx;
        margin: 0 12rpx 12rpx 0;
        color: #555;
        background: #E3E3E3;
        display: inline-block;
        width: auto;
        font-size: 28rpx;
        line-height: 80rpx;
        text-align: center;
        height: 80rpx;
    }
    
    .category-one .tab.active {
        cursor: pointer;
        border-radius: 30rpx;
        padding: 0 12rpx;
        margin: 0 12rpx 12rpx 0;
        color: #fff;
        background: linear-gradient( 317deg, #9AAEFF 0%, #9EEEEB 100%);
        display: inline-block;
        width: auto;
        font-size: 28rpx;
        line-height: 80rpx;
        text-align: center;
        height: 80rpx;
    }
</style>